package com.xiaochong.tir.trades.service;

import com.alibaba.fastjson.JSON;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.common.data.hbasepo.TradesHistory;
import com.xiaochong.tir.common.data.mapper.CurrencyInfoMapper;
import com.xiaochong.tir.common.data.mapper.KLineMapper;
import com.xiaochong.tir.common.data.po.KLine;
import com.xiaochong.tir.common.data.redisPo.KActionTotalPo;
import com.xiaochong.tir.common.data.vo.CurrencyExchangeTransaction;
import com.xiaochong.tir.common.data.vo.KlineSelectCoinSearchParams;
import com.xiaochong.tir.common.exception.hbase.HbaseCloseExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseConnectionExcetion;
import com.xiaochong.tir.common.exception.hbase.HbaseGetTableExcetion;
import com.xiaochong.tir.common.util.DateUtils;
import com.xiaochong.tir.common.util.HbaseTableUtils;
import com.xiaochong.tir.common.util.KLineTypeEnum;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.trades.component.HbaseBaseQueryComponent;
import com.xiaochong.service.common.config.redis.RedisServer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

/**
 * Created by org on 2018/6/29.
 * 选币计算
 */
@Service
public class SelectCoinQuantizationService {
    @Autowired
    private RedisServer rediscConfig ;
    @Autowired
    private CurrencyInfoMapper currencyInfoMapper;
    @Autowired
    private KLineMapper kLineMapper;
    @Autowired
    private HbaseBaseQueryComponent hbaseBaseQueryComponent;

    /**
     * 30天新高和新低、6连续下跌破位计算放入redis缓存中
     */
    public void getHighOrLowCalculate() {
        try {
            //获取当前的日期时间、计算时间
            LocalDateTime dateTime = DateUtils.dateToLocalDateTime(new Date());
            //查询货币-交易所id-交易对
            List<CurrencyExchangeTransaction> currencyInfo = currencyInfoMapper.getCurrencyExchangeTransaction();
            if(currencyInfo == null || currencyInfo.size() ==0){
                LogTrace.info("计算30天创价格新高活新低","无数据","没有启动的货币交易所交易对");
                return;
            }
            //存放超过最高价格的货币，最终放入缓存中
            List<CurrencyExchangeTransaction> currencyInfoMax = new ArrayList<>();
            //存放超过最低价格的货币，最终放入缓存中
            List<CurrencyExchangeTransaction> currencyInfoMin = new ArrayList<>();
            //存放1.2.6连续下跌破位的货币，最终放入缓存中
            List<CurrencyExchangeTransaction> currencyInfoDrop = new ArrayList<>();
            //循环计算每个货币是否符合该条件
            for(int i=0;i<currencyInfo.size();i++){
                CurrencyExchangeTransaction info = currencyInfo.get(i);
                Map<Integer,KLine> kLineMap= new HashMap<>();
                //查询30天日k数据
                for(int d =1;d <= 30; d++){
                    LocalDateTime dateCalculate = dateTime.minusDays(d);
                    String dateStr = DateUtils.dateFormatString(DateUtils.localDateTimeToDate(dateCalculate), DateUtils.format_ymd);
                    KLine kLine = new KLine();
                    kLine.setKType(KLineTypeEnum.DAY.getIndex().toString());
                    kLine.setKDate(dateStr);
                    kLine.setExchangeId(info.getExchangeId());
                    kLine.setTransationPairName(info.getTransactionName());
                    List<KLine> kLineData = kLineMapper.selectByEntityList(kLine);
                    if(kLineData != null && kLineData.size() > 0){
                        kLineMap.put(d,kLineData.get(0));
                    }
                }
                BigDecimal maxPrice = new BigDecimal(0);//30天最高价格
                BigDecimal minPrice = new BigDecimal(0);//30天最低价格
                BigDecimal lastClosePrice = new BigDecimal(0);//过去第一根收盘价格
                BigDecimal lastOpenPrice = new BigDecimal(0);//过去第一根开盘价格
                BigDecimal sumClosePrice7 = new BigDecimal(0);//7天收盘价之和
                BigDecimal sumClosePrice30 = new BigDecimal(0);//30天收盘价之和
                int ma7 =0;
                int ma30 =0;
                for(int k = 1;k <= 30; k++){
                    KLine kM = kLineMap.get(k);
                    if(kM != null){
                        BigDecimal maxPriceOld = StringUtils.isNotBlank(kM.getHighOpen())?new BigDecimal(kM.getHighOpen()):new BigDecimal(0);
                        BigDecimal minPriceOld = StringUtils.isNotBlank(kM.getLowClose())?new BigDecimal(kM.getLowClose()):new BigDecimal(0);
                        BigDecimal openPriceOld = StringUtils.isNotBlank(kM.getOpenPrice())?new BigDecimal(kM.getOpenPrice()):new BigDecimal(0);
                        BigDecimal closePriceOld = StringUtils.isNotBlank(kM.getClosePrice())?new BigDecimal(kM.getClosePrice()):new BigDecimal(0);
                        if(k == 1){
                            lastClosePrice = closePriceOld;
                            lastOpenPrice = openPriceOld;
                        }else{
                            //判断最高价格
                            if(maxPriceOld.compareTo(maxPrice) > 0){
                                maxPrice = maxPriceOld;
                            }
                            //判断最低价格
                            if(minPrice.compareTo(BigDecimal.valueOf(0)) == 0){
                                minPrice = minPriceOld;
                            }
                            if(minPriceOld.compareTo(minPrice) < 0){
                                minPrice = minPriceOld;
                            }
                        }
                        ma7++;
                        ma30++;
                        sumClosePrice7 = sumClosePrice7.add(closePriceOld);
                        sumClosePrice30 = sumClosePrice30.add(closePriceOld);
                    }
                }
                //计算a.最高价格=Max（过去第2根线到过去第30根K线的最高价） b.过去第一根K线的收盘价>最高价格
                if(lastClosePrice.compareTo(maxPrice) > 0){//最高价格符合条件
                    currencyInfoMax.add(info);
                }
                //Min（过去第2根线到过去第30根K线的最低价）过去第1根K线的收盘价<最低价
                if(lastClosePrice.compareTo(minPrice) < 0 && lastClosePrice.compareTo(BigDecimal.valueOf(0)) != 0){//最低格符合条件
                    currencyInfoMin.add(info);
                }
                //过去第1根K线的开盘价>ma7/开盘价>ma30/收盘价<ma7/收盘价<ma30
                if(ma7 != 0){
                    sumClosePrice7 = sumClosePrice7.divide(BigDecimal.valueOf(ma7),2,BigDecimal.ROUND_DOWN);
                }
                if(ma30 != 0){
                    sumClosePrice30 = sumClosePrice30.divide(BigDecimal.valueOf(ma30),2,BigDecimal.ROUND_DOWN);
                }
                if(lastOpenPrice.compareTo(sumClosePrice7) > 0 && lastOpenPrice.compareTo(sumClosePrice30) > 0
                        && lastClosePrice.compareTo(sumClosePrice7) < 0 && lastClosePrice.compareTo(sumClosePrice30) < 0){
                    currencyInfoDrop.add(info);
                }
            }
            //插入缓存中
            rediscConfig.set(RedisKeysUtils.K_ACTION_LOOK_UP_30_HIGH_PRICE, currencyInfoMax.size()>0?JSON.toJSONString(currencyInfoMax):"");
            rediscConfig.set(RedisKeysUtils.K_ACTION_LOOK_DROP_30_LOW_PRICE_DATA,currencyInfoMin.size()>0?JSON.toJSONString(currencyInfoMin):"");
            rediscConfig.set(RedisKeysUtils.K_ACTION_LOOK_DROP_CONTINUITY,currencyInfoDrop.size()>0?JSON.toJSONString(currencyInfoDrop):"");
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("计算30天创价格新高活新低",e);
        }
    }
    public void method1(String fileName) throws IOException {
        FileWriter fw = null;
        try {
//如果文件存在，则追加内容；如果文件不存在，则创建文件
            File f=new File(fileName);
            fw = new FileWriter(f, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        PrintWriter pw = new PrintWriter(fw);
        pw.println("追加内容");
        pw.flush();
        try {
            fw.flush();
            pw.close();
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 连续上涨计算1.2.3连续上涨突破
     */
    public void calculateUpLineAndChange() {
        try {
            //获取当前的日期时间、计算时间
            LocalDateTime dateTime = DateUtils.dateToLocalDateTime(new Date());
            //查询货币-交易所id-交易对
            List<CurrencyExchangeTransaction> currencyInfo = currencyInfoMapper.getCurrencyExchangeTransaction();
            if(currencyInfo == null || currencyInfo.size() ==0){
                LogTrace.info("计算均线上涨突破形态，连续上涨突破","无数据","没有启动的货币交易所交易对");
                return;
            }
            List<CurrencyExchangeTransaction> currencyInfoUpUp = new ArrayList<>();
            //循环计算每个货币是否符合该条件
            for(int i=0;i<currencyInfo.size();i++) {
                CurrencyExchangeTransaction info = currencyInfo.get(i);
                Map<String, KLine> kLineMap = new HashMap<>();
                //查询30跟12k数据
                Map<String,Object> result = this.getKDateList(15,dateTime);
                List<String> searchDate = (List<String>) result.get("searchDate");//记录k线数据
                Map<Integer, String> kLineDateMap = (Map<Integer, String>) result.get("kLineDateMap");//记录k线顺序，value为日期+type（4or5）
                if(searchDate == null || searchDate.size() == 0 || kLineDateMap == null || kLineDateMap.size() == 0){
                    continue;
                }
                List<String> searchKtype = new ArrayList<>();
                searchKtype.add(KLineTypeEnum.DAY_HALF1.getIndex().toString());
                searchKtype.add(KLineTypeEnum.DAY_HALF2.getIndex().toString());
                //查询所有12时k线 kLineMapper
                KlineSelectCoinSearchParams params = new KlineSelectCoinSearchParams();
                params.setExchangeId(info.getExchangeId());
                params.setTransationPairName(info.getTransactionName());
                params.setSearchkType(searchKtype);
                params.setSearchkDate(searchDate);
                List<KLine> searchData = kLineMapper.selectEntityListByKDateAndKType(params);
                if(searchData == null || searchData.size() == 0){
                    continue;
                }
                for(KLine kLine :searchData){ //数据组装成map
                    kLineMap.put(kLine.getKDate()+"-"+kLine.getKType(),kLine);
                }
                BigDecimal sumClosePrice7 = new BigDecimal(0);//7天收盘价之和
                BigDecimal sumClosePrice30 = new BigDecimal(0);//30天收盘价之和
                BigDecimal lastOpenPrice = new BigDecimal(0);//第一条开盘价格
                BigDecimal lastclosePrice = new BigDecimal(0);//第一条收盘价格
                int ma7=0;
                int ma30 =0;
                for(int z=1;z<=30;z++){
                    String key = kLineDateMap.get(z);
                    KLine kLine = kLineMap.get(key);
                    BigDecimal openPriceOld = new BigDecimal(0);
                    BigDecimal closePriceOld = new BigDecimal(0);
                    if(kLine != null){
                        openPriceOld = StringUtils.isNotBlank(kLine.getOpenPrice())?new BigDecimal(kLine.getOpenPrice()):openPriceOld;
                        closePriceOld = StringUtils.isNotBlank(kLine.getClosePrice())?new BigDecimal(kLine.getClosePrice()):closePriceOld;
                    }
                    if(z == 1){
                        lastOpenPrice = openPriceOld;
                        lastclosePrice = closePriceOld;
                    }
                    sumClosePrice7 = sumClosePrice7.add(closePriceOld);
                    sumClosePrice30 = sumClosePrice30.add(closePriceOld);
                    ma7++;
                    ma30++;
                }
                //判断是否符合条件过去第1根K线的开盘价<ma7，过去第1根K线的开盘价<ma30，过去第1根K线的收盘价>ma7，过去第1根K线的收盘价>ma30
                if(ma7 != 0){
                    sumClosePrice7 = sumClosePrice7.divide(BigDecimal.valueOf(ma7),2,BigDecimal.ROUND_DOWN);//ma7计算，7天平均值
                }
                if(ma30 != 0){
                    sumClosePrice30 = sumClosePrice30.divide(BigDecimal.valueOf(ma30),2,BigDecimal.ROUND_DOWN);//ma30计算，30天平均值
                }
                if(lastOpenPrice.compareTo(sumClosePrice7) < 0 && lastOpenPrice.compareTo(sumClosePrice30) < 0 &&
                   lastclosePrice.compareTo(sumClosePrice7) > 0 && lastclosePrice.compareTo(sumClosePrice30) > 0){
                    currencyInfoUpUp.add(info);
                }
            }
            rediscConfig.set(RedisKeysUtils.K_ACTION_LOOK_UP_UP_UP_PRICE,currencyInfoUpUp.size()>0?JSON.toJSONString(currencyInfoUpUp):"");
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("计算连续上涨突破",e);
        }
    }

    /**
     * 看涨-均线突破，一分钟一次
     */
    public void calculateAverageBreak() {
        try {
            //获取当前的日期时间、计算时间
            LocalDateTime dateTime = DateUtils.dateToLocalDateTime(new Date());
            //查询货币-交易所id-交易对
            List<CurrencyExchangeTransaction> currencyInfo = currencyInfoMapper.getCurrencyExchangeTransaction();
            if(currencyInfo == null || currencyInfo.size() ==0){
                LogTrace.info("看涨-均线突破","无数据","没有启动的货币交易所交易对");
                return;
            }
            List<CurrencyExchangeTransaction> currencyInfoUpUp = new ArrayList<>();
            List<CurrencyExchangeTransaction> currencyInfoDownAvg = new ArrayList<>();
            //循环计算每个货币是否符合该条件
            for(int i=0;i<currencyInfo.size();i++) {
                CurrencyExchangeTransaction info = currencyInfo.get(i);
                Map<String, KLine> kLineMap = new HashMap<>();
                //查询7天日k数据,14根数据
                Map<String,Object> result = this.getKDateList(7,dateTime);
                List<String> searchDate = (List<String>) result.get("searchDate");//记录k线数据
                Map<Integer, String> kLineDateMap = (Map<Integer, String>) result.get("kLineDateMap");//记录k线顺序，value为日期+type（4or5）
                if(searchDate == null || searchDate.size() == 0 || kLineDateMap == null || kLineDateMap.size() == 0){
                    continue;
                }
                List<String> searchKtype = new ArrayList<>();
                searchKtype.add(KLineTypeEnum.DAY_HALF1.getIndex().toString());
                searchKtype.add(KLineTypeEnum.DAY_HALF2.getIndex().toString());
                //查询所有12时k线 kLineMapper
                KlineSelectCoinSearchParams params = new KlineSelectCoinSearchParams();
                params.setExchangeId(info.getExchangeId());
                params.setTransationPairName(info.getTransactionName());
                params.setSearchkType(searchKtype);
                params.setSearchkDate(searchDate);
                List<KLine> searchData = kLineMapper.selectEntityListByKDateAndKType(params);
                if(searchData == null || searchData.size() == 0){
                    continue;
                }
                for(KLine kLine :searchData){ //数据组装成map
                    kLineMap.put(kLine.getKDate()+"-"+kLine.getKType(),kLine);
                }
                BigDecimal sumClosePriceBefore = new BigDecimal(0);//前一周期收盘价之和（1-14）
                BigDecimal sumClosePriceCurrent = new BigDecimal(0);//当前周期收盘价之和（当前价格-13）
                BigDecimal lastclosePrice = new BigDecimal(0);//第一条收盘价格
                for(int z=1;z<=14;z++){
                    String key = kLineDateMap.get(z);
                    KLine kLine = kLineMap.get(key);
                    BigDecimal closePriceOld = new BigDecimal(0);
                    if(kLine != null){
                        closePriceOld = StringUtils.isNotBlank(kLine.getClosePrice())?new BigDecimal(kLine.getClosePrice()):closePriceOld;
                    }
                    if(z == 1){
                        lastclosePrice = closePriceOld;
                    }
                    if(z <= 13){
                        sumClosePriceCurrent = sumClosePriceCurrent.add(closePriceOld);
                    }
                    sumClosePriceBefore = sumClosePriceBefore.add(closePriceOld);
                }
                //获取当前价格和，当前开盘价格
                Map<String,BigDecimal> curentPriceAll = this.getCurentPriceAll(dateTime,info.getExchangeId(),info.getTransactionName());
                BigDecimal currentPrice = curentPriceAll.get("currentPrice");//当前价格
                BigDecimal currentOpenPrice = curentPriceAll.get("currentOpenPrice");//当前开盘价格
                if(null == currentPrice || currentOpenPrice == null){
                    String showInfo = null == currentPrice?"0":"1"+"-";
                    showInfo += null==currentOpenPrice?"0":"1";
                    showInfo += "-"+info.getCmcId()+"-"+info.getExchangeId()+"-"+info.getTransactionName();
                    LogTrace.info("计算均线突破","开盘、当前价格无数据",showInfo);
                    continue;
                }
                //判断是否符合条件前一周期ma14,当前周期ma14,过去第一跟K线收盘价<前一周期ma14,当前K线的开盘价格<0.99*当前周期ma14,当前K线的即时价格>1.02*当前周期ma14
                sumClosePriceCurrent = sumClosePriceCurrent.add(currentPrice);//当前周期要加上最新价格
                sumClosePriceBefore = sumClosePriceBefore.divide(BigDecimal.valueOf(14),2,BigDecimal.ROUND_DOWN);
                sumClosePriceCurrent = sumClosePriceCurrent.divide(BigDecimal.valueOf(14),2,BigDecimal.ROUND_DOWN);
                if(lastclosePrice.compareTo(sumClosePriceBefore) < 0 && currentOpenPrice.compareTo(sumClosePriceCurrent.multiply(BigDecimal.valueOf(0.99))) < 0 &&
                        currentPrice.compareTo(sumClosePriceCurrent.multiply(BigDecimal.valueOf(1.02))) > 0 ){
                    currencyInfoUpUp.add(info);
                }
                //判断均线下跌a.过去第一跟K线收盘价>前一周期ma14,b.当前K线的开盘价格>1.01*当前周期ma14,c.当前K线的即时价格<0.98*当前周期ma14
                if(lastclosePrice.compareTo(sumClosePriceBefore) > 0 && currentOpenPrice.compareTo(sumClosePriceCurrent.multiply(BigDecimal.valueOf(1.01))) > 0 &&
                        currentPrice.compareTo(sumClosePriceCurrent.multiply(BigDecimal.valueOf(0.98))) < 0 ){
                    currencyInfoDownAvg.add(info);
                }
            }
            rediscConfig.set(RedisKeysUtils.K_ACTION_LOOK_UP_AVERAGE_BREACH_PRICE,currencyInfoUpUp.size()>0?JSON.toJSONString(currencyInfoUpUp):"");
            rediscConfig.set(RedisKeysUtils.K_ACTION_LOOK_DROP_AVERAGE_BREACH_PRICE,currencyInfoDownAvg.size()>0?JSON.toJSONString(currencyInfoDownAvg):"");
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("计算均线突破",e);
        }
    }

    /**
     * 看涨-上涨突破箱体
     */
    public void calculateBreakBottom() {
        try {
            //获取当前的日期时间、计算时间
            LocalDateTime dateTime = DateUtils.dateToLocalDateTime(new Date());
            //查询货币-交易所id-交易对
            List<CurrencyExchangeTransaction> currencyInfo = currencyInfoMapper.getCurrencyExchangeTransaction();
            if(currencyInfo == null || currencyInfo.size() ==0){
                LogTrace.info("看涨-上涨突破箱体","无数据","没有启动的货币交易所交易对");
                return;
            }
            List<CurrencyExchangeTransaction> currencyInfoUpUp = new ArrayList<>();
            List<CurrencyExchangeTransaction> currencyInfoDownBottom = new ArrayList<>();
            //循环计算每个货币是否符合该条件
            for(int i=0;i<currencyInfo.size();i++) {
                CurrencyExchangeTransaction info = currencyInfo.get(i);
                Map<String, KLine> kLineMap = new HashMap<>();
                //查询7天日k数据,9根数据
                Map<String,Object> result = this.getKDateList(5,dateTime);
                List<String> searchDate = (List<String>) result.get("searchDate");//记录k线数据
                Map<Integer, String> kLineDateMap = (Map<Integer, String>) result.get("kLineDateMap");//记录k线顺序，value为日期+type（4or5）
                if(searchDate == null || searchDate.size() == 0 || kLineDateMap == null || kLineDateMap.size() == 0){
                    continue;
                }
                List<String> searchKtype = new ArrayList<>();
                searchKtype.add(KLineTypeEnum.DAY_HALF1.getIndex().toString());
                searchKtype.add(KLineTypeEnum.DAY_HALF2.getIndex().toString());
                //查询所有12时k线 kLineMapper
                KlineSelectCoinSearchParams params = new KlineSelectCoinSearchParams();
                params.setExchangeId(info.getExchangeId());
                params.setTransationPairName(info.getTransactionName());
                params.setSearchkType(searchKtype);
                params.setSearchkDate(searchDate);
                List<KLine> searchData = kLineMapper.selectEntityListByKDateAndKType(params);
                if(searchData == null || searchData.size() == 0){
                    continue;
                }
                for(KLine kLine :searchData){ //数据组装成map
                    kLineMap.put(kLine.getKDate()+"-"+kLine.getKType(),kLine);
                }
                BigDecimal maxPrice = new BigDecimal(0);//周期内最高价格（9个开盘、9个收盘）
                BigDecimal minPrice = new BigDecimal(0);//周期内最低价格（9个开盘、9个收盘）
                for(int z=1;z<=9;z++){//计算最高最低价格
                    String key = kLineDateMap.get(z);
                    KLine kLine = kLineMap.get(key);
                    BigDecimal openPriceOld = new BigDecimal(0);
                    BigDecimal closePriceOld = new BigDecimal(0);
                    if(kLine != null){
                        openPriceOld = StringUtils.isNotBlank(kLine.getOpenPrice())?new BigDecimal(kLine.getOpenPrice()):openPriceOld;
                        closePriceOld = StringUtils.isNotBlank(kLine.getClosePrice())?new BigDecimal(kLine.getClosePrice()):closePriceOld;
                        //最高开盘、收盘的最低
                        if(openPriceOld.compareTo(maxPrice)>0){
                            maxPrice = openPriceOld;
                        }
                        if(closePriceOld.compareTo(maxPrice)>0){
                            maxPrice = closePriceOld;
                        }
                        //最低,开盘、收盘的最低
                        if(minPrice.compareTo(BigDecimal.valueOf(0)) == 0){//等于0是初始值，要替换成当前开盘或者收盘值
                            minPrice = closePriceOld;
                        }
                        if(openPriceOld.compareTo(minPrice)<0){
                            minPrice = openPriceOld;
                        }
                        if(closePriceOld.compareTo(minPrice)<0){
                            minPrice = closePriceOld;
                        }
                    }
                }
                //获取当前价格和，当前开盘价格
                Map<String,BigDecimal> curentPriceAll = this.getCurentPriceAll(dateTime,info.getExchangeId(),info.getTransactionName());
                BigDecimal currentPrice = curentPriceAll.get("currentPrice");//当前价格
                if(null == currentPrice){
                    String showInfo = null == currentPrice?"0":"1";
                    showInfo += "-"+info.getCmcId()+"-"+info.getExchangeId()+"-"+info.getTransactionName();
                    LogTrace.info("上涨突破箱体","开盘、当前价格无数据",showInfo);
                    continue;
                }
                BigDecimal bi = new BigDecimal(0);//周期内最高价/周期内最低价
                if(minPrice.compareTo(BigDecimal.valueOf(0)) != 0){
                    bi = maxPrice.divide(minPrice,2,BigDecimal.ROUND_DOWN);
                }
                //判断是否符合条件周期内最高价/周期内最低价<=1.15,当前K线的即时价格=>1.05*周期内最高价
                if(bi.compareTo(BigDecimal.valueOf(1.15)) <= 0 && currentPrice.compareTo(maxPrice.multiply(BigDecimal.valueOf(1.05)))>=0){
                    currencyInfoUpUp.add(info);
                }
                //判断跌破箱底计算：.周期内最高价/周期内最低价<=1.15 ;b.当前K线的即时价格<=0.95*周期内最高价
                if(bi.compareTo(BigDecimal.valueOf(1.15)) <= 0 && currentPrice.compareTo(maxPrice.multiply(BigDecimal.valueOf(0.95)))<=0){
                    currencyInfoDownBottom.add(info);
                }
            }
            rediscConfig.set(RedisKeysUtils.K_ACTION_LOOK_UP_BREACH_BOTTOM_PRICE,currencyInfoUpUp.size()>0?JSON.toJSONString(currencyInfoUpUp):"");
            rediscConfig.set(RedisKeysUtils.K_ACTION_LOOK_DROP_BREACH_BOTTOM_PRICE,currencyInfoDownBottom.size()>0?JSON.toJSONString(currencyInfoDownBottom):"");
        }catch (Exception e){
            e.printStackTrace();
            LogTrace.error("计算连续上涨突破",e);
        }
    }
    /**
     * 获取当前时间的开盘价格，最新价格，12小时k线
     * @param dateTime
     * @return
     */
    private Map<String,BigDecimal> getCurentPriceAll(LocalDateTime dateTime,int exchangeId,String symbol) throws Exception {
        Map<String,BigDecimal> result = new HashMap<>();
        //判断当前时间是否是12点之后，如果是取12点之后的第一条数据，否则去0点之后第一条
        String lastStr = DateUtils.localDateTimeFormatString(dateTime,DateUtils.format_ymds);
        if(dateTime.getHour() >= 12){
            String redisKeyPmOpen= RedisKeysUtils.API_SERVICE_TRADES_PM_PRIOD+"@"+exchangeId+"@"+symbol+"@"+lastStr;
            String pmOpenPrice = rediscConfig.get(redisKeyPmOpen);
            if(StringUtils.isBlank(pmOpenPrice)){
                String periodPmRowKey = HbaseTableUtils.API_SERVICE_TRADES_PM_ROWKEY+"@"+exchangeId+"@"+symbol+"@"+lastStr;
                pmOpenPrice = hbaseBaseQueryComponent.selectByQualifierData(HbaseTableUtils.API_SERVICE_TRADES_TABLE_NAME
                        , HbaseTableUtils.API_SERVICE_TRADES_TABLE_NAME_FAMILY
                        , HbaseTableUtils.API_SERVICE_TRADES_QUALIFIER
                        , periodPmRowKey);
            }
            if(StringUtils.isNotBlank(pmOpenPrice)){
                TradesHistory tradesHistory = JSON.parseObject(pmOpenPrice,TradesHistory.class);
                result.put("currentOpenPrice",tradesHistory.getPrice());//当前开盘价格
            }
        }else{//获取0点第一条交易价格
            String redisKeyPeriod = RedisKeysUtils.API_SERVICE_TRADES_PRIOD+"@"+exchangeId+"@"+symbol+"@"+lastStr;
            String basePriod = rediscConfig.get(redisKeyPeriod);
            if(StringUtils.isBlank(basePriod)) {
                String periodRowKey = HbaseTableUtils.API_SERVICE_TRADES_ROWKEY + "@" + exchangeId + "@" + symbol + "@" + lastStr;
                basePriod = hbaseBaseQueryComponent.selectByQualifierData(HbaseTableUtils.API_SERVICE_TRADES_TABLE_NAME
                        , HbaseTableUtils.API_SERVICE_TRADES_TABLE_NAME_FAMILY
                        , HbaseTableUtils.API_SERVICE_TRADES_QUALIFIER
                        , periodRowKey);
            }
            if(StringUtils.isNotBlank(basePriod)){
                TradesHistory tradesHistory = JSON.parseObject(basePriod,TradesHistory.class);
                result.put("currentOpenPrice",tradesHistory.getPrice());//当前开盘价格
            }
        }
        //最新价格
        String changeRedisKey = RedisKeysUtils.API_SERVICE_K_ACTION_CHANGE_PRICE+"@"+exchangeId+"@"+symbol+"@"+lastStr;
        String currentStr =  rediscConfig.get(changeRedisKey);
        if(StringUtils.isNotBlank(currentStr)){
            KActionTotalPo kActionTotalPo = JSON.parseObject(currentStr,KActionTotalPo.class);
            if(kActionTotalPo != null && kActionTotalPo.getLastOpenPrice() != null){
                result.put("currentPrice",new BigDecimal(kActionTotalPo.getLastOpenPrice()));
            }
        }
        return result;
    }

    /**
     * 根据天数，计算要查询k线的日期，以及k线顺序
     * @param search
     * @param dateTime
     * @return
     */
    public Map getKDateList(int search,LocalDateTime dateTime){
        Map<String,Object> result = new HashMap<>();
        Map<Integer, String> kLineDateMap = new HashMap<>();//记录k线顺序，value为日期+type（4or5）
        List<String> searchDate = new ArrayList<>();
        int count =0;
        for(int d =0;d <= search; d++){ //需要查询的日期
            LocalDateTime dateCalculate = dateTime.minusDays(d);
            String dateStr = DateUtils.dateFormatString(DateUtils.localDateTimeToDate(dateCalculate), DateUtils.format_ymd);
            searchDate.add(dateStr);
            if(dateCalculate.getHour()>=12 && d == 0){
                count++;
                kLineDateMap.put(count,dateStr+"-"+4);
            }
            if(d > 0){
                count++;
                kLineDateMap.put(count,dateStr+"-"+5);
                count++;
                kLineDateMap.put(count,dateStr+"-"+4);
            }
        }
        result.put("searchDate",searchDate);
        result.put("kLineDateMap",kLineDateMap);
        return  result;
    }



}
